package cc.jinglupeng.kom.util;

import cc.jinglupeng.kom.pool.ZookeeperPool;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by jinglupeng on 4/10/16.
 */
public class ZkUtils {

    private static Logger logger = LoggerFactory.getLogger(ZkUtils.class);

    public static String getData(String path) {
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = ZookeeperPool.borrowZooKeeper();
            return getData(zooKeeper, path);
        } catch (Exception e) {
            logger.error("borrow zookeeper failed", e);
            return null;
        } finally {
            ZookeeperPool.returnZooKeeper(zooKeeper);
        }
    }

    public static <T> T getData(String path, Class<T> clazz) {
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = ZookeeperPool.borrowZooKeeper();
            return getData(zooKeeper, path, clazz);
        } catch (Exception e) {
            logger.error("borrow zookeeper failed", e);
            return null;
        } finally {
            ZookeeperPool.returnZooKeeper(zooKeeper);
        }
    }

    public static List<String> getChildren(String path) {
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = ZookeeperPool.borrowZooKeeper();
            return getChildren(zooKeeper, path);
        } catch (Exception e) {
            logger.error("borrow zookeeper failed", e);
            return null;
        } finally {
            ZookeeperPool.returnZooKeeper(zooKeeper);
        }
    }

    public static String getData(ZooKeeper zooKeeper, String path) {
        try {
            byte[] data = zooKeeper.getData(path, null, null);
            if (data == null) return null;
            return new String(data, "UTF-8");
        } catch (Exception e) {
            logger.error("get data failed, path {}", path);
            return null;
        }
    }

    public static <T> T getData(ZooKeeper zooKeeper, String path, Class<T> clazz) {
        String json = getData(zooKeeper, path);
        if (json == null) return null;
        return JsonUtil.fromJson(json, clazz);
    }

    public static List<String> getChildren(ZooKeeper zooKeeper, String path) {
        try {
            return zooKeeper.getChildren(path, null);
        } catch (Exception e) {
            logger.error("get children failed, path {}", path);
            return null;
        }
    }

    public static Stat getStat(String path) {
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = ZookeeperPool.borrowZooKeeper();
            return getStat(zooKeeper, path);
        } catch (Exception e) {
            logger.error("borrow zookeeper failed", e);
            return null;
        } finally {
            ZookeeperPool.returnZooKeeper(zooKeeper);
        }
    }

    public static Stat getStat(ZooKeeper zooKeeper, String path) {
        try {
            return zooKeeper.exists(path, null);
        } catch (Exception e) {
            logger.error("get data failed, path {}", path);
            return null;
        }
    }

}
